<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Formmatch extends Common
{
	public function add(){
		return $this->fetch();
	}

	public function edit($id){
	   	$result = Db::name("contests")->where('co_id',$id)->find();
    	$this->assign("result",$result);
    	return $this->fetch();
	}

  public function jiekou(){
        $rs=Db::name('contests')->select();
        return json($rs);
  }

	public function addMatch(){
		$info = '';

       	if (request()->isPost()) {
          $result = Db::name('contests')->where('co_name',input('param.co_name'))->find();
          if($result){
             $this->error("比赛名重复",'formmatch/add');
          }
          
             
          $file = request()->file('pic');
          $data=array();
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
              $data=[
                  'co_name'=>input('param.co_name'),
                  'co_intro'=>input('param.co_intro'),
                  'co_address'=>input('param.co_address'),
                  'co_price'=>input('param.co_price'),
                  'co_date'=>input('param.co_date'),
                  'co_poster'=>$info->getFilename(),
       		     ];
          }else{
          	$data=[
                  'co_name'=>input('param.co_name'),
                  'co_intro'=>input('param.co_intro'),
                  'co_address'=>input('param.co_address'),
                  'co_price'=>input('param.co_price'),
                  'co_date'=>input('param.co_date'),
                  'co_poster'=>'default.jpg',
       		     ];
          }
        $res=Db::name('contests')->insert($data);
        
        //处理结果
        if ($res) {
        	$this->success("添加比赛成功",'match/index');
        }
        else {
        	$this->error("添加比赛失败",'match/index');
        }
       	}
	}
	
	public function updatematch($id){
		 $info = '';
        if (request()->isPost()) {
          //创建数组，用video表的所有字段赋值
          $result = Db::name('contests')->where('co_name',input('param.co_name'))->find();
          /*if($result){
             $this->error("活动名字重复",'activity/index');
          }*/
         
          $file = request()->file('pic');
          $pic="";
          $pic2=input('pic2');//图片没改之前的名字
          $data=array();
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
                
              if($info){
                $pic = $info->getFilename();
                 if($result){
                      $data=[
                        'co_intro'=>input('param.co_intro'),
                        'co_address'=>input('param.co_address'),
                        'co_price'=>input('param.co_price'),
                        'co_date'=>input('param.co_date'),
                        'co_poster'=>$info->getFilename(),
                     ];
                 }else{
                    $data=[
                      'co_name'=>input('param.co_name'),
                      'co_intro'=>input('param.co_intro'),
                      'co_address'=>input('param.co_address'),
                      'co_price'=>input('param.co_price'),
                      'co_date'=>input('param.co_date'),
                      'co_poster'=>$info->getFilename(),
                   ];
                 }
              }else{
                echo $file->getError();
              }
          }
          else{
            if($result){
                      $data=[
                        'co_intro'=>input('param.co_intro'),
                        'co_address'=>input('param.co_address'),
                        'co_price'=>input('param.co_price'),
                        'co_date'=>input('param.co_date'),
                     ];
                 }else{
                    $data=[
                      'co_name'=>input('param.co_name'),
                      'co_intro'=>input('param.co_intro'),
                      'co_address'=>input('param.co_address'),
                      'co_price'=>input('param.co_price'),
                      'co_date'=>input('param.co_date'),
                   ];
                 }
        }

          

          if(!empty($pic)&&$pic2!='default.jpg'){
            if(file_exists(ROOT_PATH . 'public' . DS . 'static'. DS .'posters'.DS.$pic2)){
              unlink(ROOT_PATH . 'public' . DS . 'static'. DS .'posters'.DS.$pic2);
            }
          }
          
         
        $res=Db::name('contests')->where("co_id",$id)->update($data);
        //处理结果
        if ($res!==false) {
          $this->success("修改比赛成功",'match/index');
        }
        else {
          $this->error("修改比赛失败",'match/index');
        }
      }
	}

	public function delMatch($id){
		
        if(Db::name("contests")->delete($id)){
          $this->success('删除成功','match/index');
        }else 
          $this->error('删除失败','match/index');      
    }
	
}
